package James;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;

public class JTableExploration 
{
	private JScrollPane jsp;
	private JTable myTable, myDBTable;
	private Connection myCon;
	private ResultSet rs;
	private ResultSetMetaData rsmd;
	
	public static void main(String [] args)
	{
		new JTableExploration();
	}
	public JTableExploration()
	{
		try
		{
			createDBCon();
			createDBTable("SELECT * FROM pet;");
			createDBGUI();
			//rs.next();
			//System.out.println(rs.getString(2));
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		
		//createGUI();
	}
	
	public void createTable()
	{
		String [] columnNames = new String[]{
			"Name", "Surname", "Sport", "Age", "Vegetarian"};
		Object [] [] data = new Object[][]{
		{"James", "Kukard", "Extreme Ironing", new Integer(24), new Boolean(false)},
		{"Christopher", "Buckley", "Jogging", new Integer(24), new Boolean(false)},
		{"Francois", "Grogor", "Sleep walking", new Integer(22), new Boolean(true)},
		{"Darren", "Olivier", "Tiddlywinks", new Integer(22), new Boolean(false)},
		};
		
		//Create JTable
		myTable = new JTable(data, columnNames);
		//myTable.setPreferredScrollableViewportSize(new Dimension(300,200));
	}
	
	public void createDBTable(String query) throws SQLException
	{
		rs = getRS(query);
		Vector headVect, dataVect;
		headVect = createHeadingVect(rs);
		dataVect = createDataVect(rs);
		System.out.println(headVect);
		System.out.println(dataVect);
		myDBTable = new JTable(dataVect, headVect);
		TableColumn column = null;
		for(int i = 0; i < 6; i++)
		{
			column = myDBTable.getColumnModel().getColumn(i);
			if(i == 2 || i == 5)
			{
				column.setPreferredWidth(100);
			}
			else
			{
				column.setPreferredWidth(50);
			}
		}
		myDBTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		
		
	}
	
	public void createGUI()
	{
		//Create GUI to display table (what a mission.)
		jsp = new JScrollPane(myTable);
		jsp.getViewport().setOpaque(true);//What the hell does this do?
		//myTable.setFillsViewportHeight(true);//Not available in Java SE 5.
		
		//jsp.add();
		
		JFrame frame = new JFrame();
		frame.setSize(800,250);
		frame.setLocationRelativeTo(null);
		//frame.getContentPane().add(myTable.getTableHeader(), BorderLayout.PAGE_START);
		frame.getContentPane().add(jsp, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public void createDBGUI()
	{
		//Create GUI to display table (what a mission.)
		jsp = new JScrollPane(myDBTable);
		//jsp.getViewport().setOpaque(true);//What the hell does this do?
		//myTable.setFillsViewportHeight(true);//Not available in Java SE 5.
		
		//jsp.add();
		
		JFrame frame = new JFrame();
		frame.setSize(600,250);
		frame.setLocationRelativeTo(null);
		//frame.getContentPane().add(myTable.getTableHeader(), BorderLayout.PAGE_START);
		frame.getContentPane().add(jsp, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public void createDBCon()
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			myCon = DriverManager.getConnection("jdbc:mysql://localhost/menagerie", "", "");
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		
	}
	
	public ResultSet getRS(String query) throws SQLException
	{
		ResultSet resSet;
		Statement stmt = myCon.createStatement(
			ResultSet.TYPE_SCROLL_SENSITIVE,
			ResultSet.CONCUR_READ_ONLY);
		resSet = stmt.executeQuery(query);
		return resSet;
	}
	
	public Vector createDataVect(ResultSet rs) throws SQLException
	{
		Vector vect = new Vector();
		while(rs.next())
		{
			Vector innerVect = new Vector();
			int i = 1;
			//System.out.println("rs.getString(i): " + rs.getString(i));
			//System.out.println("rs.getString(i).length(): " + rs.getString(i).length());
			while(i < 7)
			{
				innerVect.add(rs.getString(i));
				i++;
			}
			vect.add(innerVect);
		}
		return vect;
	}
	
	public Vector createHeadingVect(ResultSet rs) throws SQLException
	{
		ResultSetMetaData resMet = rs.getMetaData();
		Vector vect = new Vector(resMet.getColumnCount());
		int i = 1;
		while(i <= resMet.getColumnCount())
		{
			System.out.println(resMet.getColumnName(i));
			i++;
		}
		i=1;
		while(i <= resMet.getColumnCount())
		{
			vect.add(resMet.getColumnName(i));
			i++;
		}
		
		return vect;
	}
	
}
